#include <cmath>
#include <algorithm>
#include "mode16.h"

struct node
{
   float Hval;
   int type;
   node *next;
}

void insert(node* head, float val, int i)
{
    node* p = new node;
    p->Hval = val;
    p->type = i;
    p->next = head;
    head = p;
}

void sort(node* head)
{
	
	node *temp1 = new node;                         // create a temporary node
	node *temp2 = new node;                         // create a temporary node
 
    float temph = 0;
	int tempt = 0;                        // store temporary data value
 
	for( temp1 = head ; temp1!=NULL ; temp1 = temp1->next )
	{
		  for( temp2 = temp1->next ; temp2!=NULL ; temp2 = temp2->next )
		  {
				if( temp1->Hval > temp2->Hval )
				{
					  temph = temp1->Hval;
					  temp1->Hval = temp2->Hval;
					  temp2->Hval = temph;
					  
					  tempt = temp1->type;
					  temp1->type = temp2->type;
					  temp2->type = tempt;
				}
		  }
    }
}
}
int mode16::dir16(float* dc, float S)
{
    float *H = new float[4];
    node* temp = new node;
    
    H[0] = S/2;                                     //Hdc
    H[1] = abs(dc[0]-dc[2]) + abs(dc[1]-dc[3]);     //Hv
    H[2] = abs(dc[0]-dc[1]) + abs(dc[2]-dc[3]);     //Hh
    H[3] = abs(dc[0]-dc[3]) + abs(dc[1]-dc[2]);     //Hp
    
    for(int i=0; i<4; i++)
      insert(temp, H[i], i);
    
    sort(temp);
    
    switch (temp->type) {
       case 0:
       {
		   std::cout << "The direction is DC" <<std::endl;
		   return 0;
           break;
       }
       case 1:
       {
           std::cout << "The direction is Vertical" <<std::endl;
           return 1;
           break;
        }
       case 2:
       {
           std::cout << "The direction is Horizontal" <<std::endl;
           return 2;
           break;
        }
       default:
       {
           std::cout << "The direction is Plane" << std::endl;
           return 3;
           break;
        }
}      
    
    